<template>
  <div class="security">
    <el-breadcrumb separator-class="el-icon-arrow-right" style="box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);line-height: 3;padding-left: 1em;">
      <el-breadcrumb-item>系统管理</el-breadcrumb-item>
      <el-breadcrumb-item>安全信息</el-breadcrumb-item>
    </el-breadcrumb>
    <div class="mobile">
      <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="手机号" prop="mobile">
          <el-input  v-model.number="ruleForm.mobile"  style="width: 60%;" placeholder="请输入手机号" disabled></el-input>
          <router-link to="/modifymember">
            <el-button type="primary" plain>更换绑定手机号</el-button>
          </router-link>
        </el-form-item>
         <el-form-item label="原密码" prop="usedpass">
          <el-input type="password" v-model="ruleForm.usedpass" autocomplete="off" style="width: 60%;" placeholder="请输入原密码"></el-input>
        </el-form-item>
        <el-form-item label="新密码" prop="pass">
          <el-input type="password" v-model="ruleForm.pass" autocomplete="off" style="width: 60%;" placeholder="请输入新密码"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="checkPass">
          <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off" style="width: 60%;" placeholder="请再次出入密码"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
  export default {
    data() {
      var checkAge = (rule, value, callback) => {
        if (!value) {
          return callback(new Error('手机不能为空'));
        }
        setTimeout(() => {
          if (!Number.isInteger(value)) {
            callback(new Error('请输入数字值'));
          } else {
            if (!(/^1[34578]\d{9}$/.test(value))) {
              callback(new Error('请输入正确手机号'));
            } else {
              callback();
            }
          }
        }, 1000);
      };
      var validateusedpass = (rule, value, callback) => {
         if (value === '') {
          callback(new Error('请输入原密码'));
        }
      };
      var validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入新密码'));
        } else {
          if (this.ruleForm.checkPass !== '') {
            this.$refs.ruleForm.validateField('checkPass');
          }
          callback();
        }
      };
      var validatePass2 = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请再次输入密码'));
        } else if (value !== this.ruleForm.pass) {
          callback(new Error('两次输入密码不一致!'));
        } else {
          callback();
        }
      };
      return {
        ruleForm: {
          pass: '',
          checkPass: '',
          mobile: this.geTel("13130000000"),
          usedpass:''
        },
        rules: {
          usedpass:[{
            required: true,
            validator: validateusedpass,
            trigger: 'blur'
          }],
          pass: [{
            required: true,
            validator: validatePass,
            trigger: 'blur'
          }],
          checkPass: [{
            required: true,
            validator: validatePass2,
            trigger: 'blur'
          }],
          mobile: [{
            required: true,
            validator: checkAge,
            trigger: 'blur'
          }]
        }
      };
    },
    methods: {
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            alert('submit!');
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      },
      geTel(tel) { //隐藏手机号中间4位
      	var reg = /^(\d{3})\d{4}(\d{4})$/;
      	return tel.replace(reg, "$1****$2");
      },
    }
  }
</script>

<style scoped="scoped">
  .mobile {
    width: 700px;
    height: 300px;
    margin: 30px;
    padding: 60px;
    box-shadow: 0 2px 4px rgba(0, 0, 0, .5), 0 0 6px rgba(0, 0, 0, .04);
  }
</style>
